var str = '345CD';
var str1 = 'CD'

function LCS(str,str1) {
  var minLen = Math.max(str.length, str1.length)
  var x = 0,
    y = 0;

  var result = []
  while (y < minLen) {
    if (x === minLen) {
      x = 0;
      y++;
    }
    var temX = str[x]
    var temY = str1[y]
    let r = [];
    if (temX === temY) {
      let xx = x;
      let yy = y;
      r.push([xx, yy])
      while (xx < minLen && yy < minLen) {
        ++xx;
        ++yy;
        if (str1[yy] && str[xx] && str[xx] === str1[yy]) {
          r.push([xx, yy])
        } else {
          break;
        }
      }
      if (r.length > result.length) {
        result = r;
      }
    }
    x++;
  }
  var ss = ''
  for (let index = 0; index < result.length; index++) {
    ss += str[result[index][0]] ? str[result[index][0]] : ''
  }
  return ss;
}

console.log(LCS(str, str1))

//   3 4 5 C D
// C 0 0 0 1 0
// D 0 0 0 0 1
// 3 1 0 0 0 0
// 4 0 1 0 0 0

// var x = 0;
// var y = 0;

// [[0,0],[1,1],[2,2],[3,3]] 
// [[1,0],[2,1],[3,2],[4,3]] 
// [[2,0],[3,1],[4,2]] 
// [[0,1],[1,2],[2,3]] 